// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
  throwOnNotFound: false
})
const db = cloud.database();
const _ = db.command;
const $ = _.aggregate;

searchCustomer = async(event) => {
  
  let exp = db.RegExp({
    regexp: event.name,
  });
  let where = _.and([
    {
      studioid: event.studioid,
    },
    _.or([{name: exp}, {phone: exp}, {nickName: exp}])]);
  return await db.collection('customer').aggregate()
  .match({
    studioid: event.studioid,
  }).match(
    _.or([
      {
        name: exp
      },
      {
        nickName: exp
      },
      {
        phone: exp
      },
    ])
  ).end();
}

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.op) {
    case 'search':
      return searchCustomer(event);
   
  }
}